//package com.innjia.shiro.realm;
//
//import com.innjia.base.common.Constant;
//import com.innjia.sys.entity.UserEntity;
//import com.innjia.sys.service.UserService;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.cas.CasRealm;
//import org.apache.shiro.cas.CasToken;
//import org.jasig.cas.client.authentication.AttributePrincipal;
//import org.jasig.cas.client.validation.Assertion;
//import org.jasig.cas.client.validation.TicketValidationException;
//import org.jasig.cas.client.validation.TicketValidator;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.List;
//
///**
// * 类的功能描述.
// * 自定义casRealm
// * @auther innjia
// * @Date 2017/4/27
// */
//
//public class MyCasRealm extends CasRealm {
//    @Autowired
//    private UserService userService;
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        CasToken casToken = (CasToken) token;
//        if(casToken==null){
//            return null;
//        }
//        String ticket = (String) casToken.getCredentials();
//        if(StringUtils.isEmpty(ticket)){
//            return null;
//        }
//        TicketValidator ticketValidator = ensureTicketValidator();
//        Assertion assertion =null;
//        try {
//            assertion = ticketValidator.validate(ticket,getCasService());
//            AttributePrincipal casPrincipal = assertion.getPrincipal();
//            String userLoginName = casPrincipal.getName();
//            UserEntity user = userService.queryByLoginName(userLoginName);
//            if(user == null){
//                throw new AuthenticationException("帐号密码错误");
//            }
//            if(Constant.ABLE_STATUS.NO.getValue().equals(user.getStatus())){
//                throw new AuthenticationException("帐号被禁用,请联系管理员!");
//            }
//            //用户对应的机构集合
//            List<String> baidList = userService.queryBapidByUserIdArray(user.getId());
//            //用户对应的部门集合
//            List<String> bapidList= userService.queryBaidByUserIdArray(user.getId());
//            user.setBapidList(bapidList);
//            user.setBaidList(baidList);
//            return new SimpleAuthenticationInfo(user, ticket, getName());
//        } catch (TicketValidationException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//}
